<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="分组名称" prop="groupName">
        <el-input
          v-model="queryParams.groupName"
          placeholder="请输入分组名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>

    <!-- 数据表格 -->
    <el-table v-loading="loading" :data="customerGroupList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="分组ID" prop="groupId" width="120" />
      <el-table-column label="分组名称" prop="groupName" width="150" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加/编辑弹窗 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="分组名称" prop="groupName">
          <el-input v-model="form.groupName" placeholder="请输入分组名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCustomerGroup, addCustomerGroup, updateCustomerGroup, deleteCustomerGroupById } from "@/api/customer/group";
import { resetFields } from "element-ui";
export default {
  name: "CustomerGroupIndex",
  data() {
    return {
      loading: true,
      customerGroupList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupName: undefined
      },
      total: 0,
      single: true,
      multiple: true,
      open: false,
      title: "",
      form: {},
      rules: {
        groupName: [
          { required: true, message: "分组名称不能为空", trigger: "blur" }
        ]
      },
      showSearch: true // 添加showSearch属性并设置初始值
    };
  },

  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listCustomerGroup(this.queryParams).then(response => {
        if (response.code === 200) { // 检查返回的状态码是否正确
          this.customerGroupList = response.rows; // 使用 response.rows 来设置数据
          this.total = response.total; // 设置总记录数
          this.loading = false;
        } else {
          this.$message.error(response.msg); // 显示错误消息
        }
      }).catch(error => {
        this.$message.error('请求出错'); // 处理请求错误
        console.error(error);
      });
    },
    // 其他方法...


  handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.$refs.queryForm.resetFields();
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.single = selection.length !== 1;
      this.multiple = selection.length === 0;
    },
    handleAdd() {
      if (this.$refs.queryForm && this.$refs.queryForm.resetFields) {
        this.$refs.queryForm.resetFields(); // 调用 resetFields 方法
      }
      this.open = true;
      this.title = "新增分组";
    },
    handleUpdate(row) {
      this.form = Object.assign({}, row);
      this.open = true;
      this.title = "修改分组";
    },
    cancel() {
      this.open = false;
      this.resetForm();
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.groupId) {
            updateCustomerGroup(this.form).then(() => {
              this.$message.success("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustomerGroup(this.form).then(() => {
              this.$message.success("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDelete(row) {
      this.$confirm("确认删除该分组？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          const groupId = [row.groupId];
          deleteCustomerGroupById(groupId, "post").then(() => {
            this.$message.success("删除成功");
            this.getList();
          });
        })
        .catch(() => {});
    },

    resetForm() {
      this.$refs.form.resetFields();
      this.form = {};
    }
  }
};
</script>
